package com.example.demo.controller;

import com.example.demo.annotation.Log;
import com.example.demo.annotation.RequestAuthCheck;
import com.example.demo.config.ConstantConfig;
import com.example.demo.entity.BaseEntity;
import com.example.demo.entity.Menu;
import com.example.demo.entity.dto.PageDto;
import com.example.demo.entity.vo.PageVo;
import com.example.demo.entity.vo.ResponseResult;
import com.example.demo.enums.StatusCodeEnum;
import com.example.demo.enums.log.BusinessTypeEnum;
import com.example.demo.enums.log.OperatorTypeEnum;
import com.example.demo.service.MenuService;
import com.example.demo.util.MyExcelUtil;
import com.example.demo.util.NormalUtil;
import com.example.demo.util.TreeUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @description：菜单相关接口控制器.
 * @author：lw
 * @create_time：2024/1/8
 **/


@Tag(name="菜单接口")
@ApiSupport(order = 3)
@RestController
@RequestMapping(value = "/admin/menu")
public class MenuController {
    @Autowired
    MenuService menuService;

    private final String menuName="menu";

    @Operation(summary = "新增")
    @ApiOperationSupport(order = 1,author = "lw")
    @RequestAuthCheck(menuName = menuName,action = ConstantConfig.add)
    @Log(title = "新增菜单",businessType = BusinessTypeEnum.add,operatorType = OperatorTypeEnum.admin_user)
    @PostMapping(value = "/add")
    public ResponseResult<Boolean> add(@RequestBody List<Menu> list) {
//        return  ResponseResult.build(menuService.save(menu),StatusCodeEnum.add_success);
        return  ResponseResult.build(menuService.saveBatch(list),StatusCodeEnum.add_success);
    }

    @Operation(summary = "删除")
    @ApiOperationSupport(order = 2,author = "lw")
    @RequestAuthCheck(menuName = menuName,action = ConstantConfig.delete)
    @Log(title = "删除菜单",businessType = BusinessTypeEnum.delete,operatorType = OperatorTypeEnum.admin_user)
    @PostMapping(value = "/deleteByIds")
    public ResponseResult<Boolean> deleteByIds(@RequestBody List<Long> ids) {
        //        UpdateWrapper<Menu> updateWrapper=new UpdateWrapper<>();
//        updateWrapper.in("id",ids);
//        updateWrapper.set("is_deleted",1);
//        return  ResponseResult.build(menuService.update(updateWrapper),StatusCodeEnum.delete_success);
        return  ResponseResult.build(menuService.removeByIds(ids),StatusCodeEnum.delete_success);
    }

    @Operation(summary = "更新")
    @ApiOperationSupport(order = 3,author = "lw")
    @RequestAuthCheck(menuName = menuName,action = ConstantConfig.update)
    @Log(title = "更新菜单",businessType = BusinessTypeEnum.update,operatorType = OperatorTypeEnum.admin_user)
    @PostMapping(value = "/update")
    public ResponseResult<Boolean> update(@RequestBody List<Menu> list) {
//        return  ResponseResult.build(menuService.updateById(menu),StatusCodeEnum.update_success);
        return  ResponseResult.build(menuService.updateBatchById(list),StatusCodeEnum.update_success);
    }

    @Operation(summary = "获取详情")
    @ApiOperationSupport(order = 4 ,author = "lw")
    @GetMapping(value = "/detail")
    public ResponseResult<Menu> getDetail(Long id) {
        return  ResponseResult.build(menuService.getById(id),StatusCodeEnum.success);
    }

    @Operation(summary = "列表查询")
    @ApiOperationSupport(order = 5,author = "lw")
    @PostMapping(value = "/list")
    public ResponseResult<List<Menu>> getList(@RequestBody Menu menu) {
        return  ResponseResult.build(menuService.getList(menu),StatusCodeEnum.query_success);
    }

    @Operation(summary = "分页查询")
    @ApiOperationSupport(order = 6,author = "lw")
    @PostMapping(value = "/page")
    public ResponseResult<PageVo<Menu>> getPage(@RequestBody PageDto<Menu> pageDto) {
        return  ResponseResult.build(menuService.getPage(pageDto),StatusCodeEnum.query_success);
    }

    @Operation(summary = "导出")
    @ApiOperationSupport(order = 7,author = "lw")
    @RequestAuthCheck(menuName = menuName,action = ConstantConfig.export)
    @PostMapping(value = "/export")
    public void export(@RequestBody PageDto<Menu> pageDto, HttpServletResponse response) {
        PageVo<Menu> pageVo=menuService.getPage(pageDto);
        MyExcelUtil.export(response,pageVo, NormalUtil.mergeMaps(Menu.getExcelCols(), BaseEntity.getExcelCols()),Menu.entityTitle);
    }

    @Operation(summary = "导入")
    @ApiOperationSupport(order = 8 ,author = "lw")
    @RequestAuthCheck(menuName = menuName,action = ConstantConfig.Import)
    @PostMapping(value = "/import")
    public ResponseResult<List<Menu>>  importList(@RequestPart("file") MultipartFile file){
        List<Menu> list=MyExcelUtil.read(file,Menu.class);
        return ResponseResult.build(list,StatusCodeEnum.import_success);
    }

    @Operation(summary = "获取完整菜单树")
    @ApiOperationSupport(order = 9,author = "lw")
    @PostMapping(value = "/getMenuTree")
    public ResponseResult<Menu> getMenuTree() {
        return  ResponseResult.build(TreeUtil.listToTree(menuService.list(),Menu.rootId,Menu.id_key,Menu.parentId_key,Menu.sort_key,Menu.class),StatusCodeEnum.success);
    }
}
